package com.fpost.helper;

import com.fpost.form.dto.ArticleCommentDTO;
import com.fpost.form.dto.ArticleDTO;
import com.fpost.form.dto.ArticleVersionDTO;

public class Byline {
	
	private String author;
	private String timestamp;
	private String ip;
	private String rating;
	private String hitCount;

	public Byline(Object obj) {
		
		if (obj instanceof ArticleDTO) {
			ArticleDTO articleDTO = (ArticleDTO)obj;
			setAuthor(articleDTO.getAuthorFirstName(), articleDTO.getAuthorLastName(), articleDTO.getAuthorUsername()); 
			setTimestamp(articleDTO.getCreatedOn());
			setIp(articleDTO.getCreatedIp());
			setRating(articleDTO.getAverageRating(), articleDTO.getNumberOfRatings());
			setHitCount(articleDTO.getHitCount());
		
		} else if (obj instanceof ArticleCommentDTO) {
			ArticleCommentDTO articleCommentDTO = (ArticleCommentDTO)obj;
			setAuthor(articleCommentDTO.getFirstName(), articleCommentDTO.getLastName(), articleCommentDTO.getUsername()); 
			setTimestamp(articleCommentDTO.getCreatedOn());
			setIp(articleCommentDTO.getCreatedIp());

		} else if (obj instanceof ArticleVersionDTO) {
			ArticleVersionDTO articleVersionDTO = (ArticleVersionDTO)obj;
			setAuthor(articleVersionDTO.getAuthorFirstName(), articleVersionDTO.getAuthorLastName(), articleVersionDTO.getAuthorUsername()); 
			setTimestamp(articleVersionDTO.getCreatedOn());
			setIp(articleVersionDTO.getCreatedIp());
			setHitCount(articleVersionDTO.getHitCount());
		}
	}	
	
	public String getAuthor() {
		return author;
	}
	private void setAuthor(String firstName, String lastName, String username) {
		this.author = "by" + (firstName == null ? "" : (" " + firstName)) + 
				(lastName == null ? "" : (" " + lastName)) + " (" + username + ") ";
	}	
	public String getTimestamp() {
		return timestamp;
	}
	private void setTimestamp(String timestamp) {
		this.timestamp = timestamp + " ";
	}
	public String getIp() {
		return ip;
	}
	private void setIp(String ip) {
		this.ip = " IP:" + ip + " ";
	}
	public String getRating() {
		return rating;
	}
	private void setRating (String averageRating, String numberOfRatings) {
		this.rating = "Article rating: " + 
	("-1.0".equals(averageRating) ? "not rated " : averageRating + " out of 10, rated by " + numberOfRatings + " people. ");
	}
	public String getHitCount() {
		return hitCount;
	}
	private void setHitCount(String hitCount) {
		this.hitCount = "Hit count " + hitCount + " ";
	}
	
}
